var connect = {
    uExId     : 0,
    uId       : 0,
    timestamp : 0,
    noerror   : false,
    code      :   1,
    startCall : function(){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/startCall',
            data: {
                'uEx'       : connect.uExId
            },
            dataType: 'json',
            success: function(data){
                var htmlReturn = '';
                if(data.code == 0){
                    // dang ban tu van
                    htmlReturn = '<span>'+data.uName+' hiện đang bận. Bạn có muốn đăng ký vào danh sách chờ hay không?</span>';
                    htmlReturn += '<p><input type="button" value="Bỏ qua" class="peding-call-cancel" /><input type="button" value="Ðồng ý" class="pending-call-ok" /></p>';
                    htmlReturn += '<input type="hidden" class="uId" value="'+data.uid+'"/>';
                    connect.handleResponse(htmlReturn,0);
                }else if(data.code == 1){
                    // cho duoc tu van
                    htmlReturn = '<span>Đang thực hiện kết nối</span>';
                    connect.handleResponse(htmlReturn,1);
                }else{
                    htmlReturn = '<span>Kết nối không hợp lệ!</span>';
                    connect.handleResponse(htmlReturn,1);
                }
            }
        });
    },
    savePendingCall : function(){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/savePedingCall',
            data: {
                'uEx'       : connect.uExId
            },
            dataType: 'json',
            success: function(data){
                var htmlReturn = '';
                htmlReturn = '<span>Thông tin đã được lưu thành công!</span>';
                connect.handleResponse(htmlReturn,1);
            }
        });
    },
    checkCallExpert : function(){
        //run realtime check call
        $.ajax({
            type: 'GET',
            url: '/CallUser/checkCallExpert',
            data: {
                'timestamp' : connect.timestamp
            },
            dataType: 'json',
            success: function(data){
                if(data.code == 1){
                    connect.timestamp = data.timescreate;
                    if(data.typeMsg == 1){
                        var htmlReturn = '<span>'+data.unameCall +' đã yêu cầu liên hệ với bạn. Nếu bạn đã bắt đầu tư vấn cho '+data.unameCall +' thì hãy click vào đây để yêu cầu '+data.unameCall +' thanh toán!</span><p><span class="time">Lúc '+data.timescreate+'</span><input type="button" value="Bỏ qua" class="cofirm-call-cancel" /><input type="button" value="Đồng ý" class="cofirm-call-ok" /></p>';
                        htmlReturn += '<input type="hidden" class="uid" value="'+data.uId+'"/>'; // user thuc hien call
                        connect.handleResponse(htmlReturn,0);
                    }else if(data.typeMsg == 2){
                        var htmlReturn = '<span>Tư vấn đã kết thúc!</span>';
                        connect.handleResponse(htmlReturn,2);
                    }else{
                        var htmlReturn = '<span>Yêu cầu thanh toán của bạn bị từ chối!</span>';
                        connect.handleResponse(htmlReturn,1);
                    }
                }else if(data.code == 2){
                    connect.timestamp = data.timescreate;
                    var htmlReturn = '<span>Hệ thống bắt đầu tính phí!</span>';
                    htmlReturn += '<p><input type="button" value="Ngừng" class="close_call" /></p>';
                    htmlReturn += '<input type="hidden" class="userId" value="'+data.uExpertId+'"/>';
                    htmlReturn += '<input type="hidden" class="uidCall" value="'+data.uId+'"/>';//user thuc hien call
                    htmlReturn += '<input type="hidden" class="transactionId" value="'+data.transactionId+'"/>';//transaction id payment
                    htmlReturn += '<input type="hidden" class="typePayment" value="'+data.typePayment+'"/>';//payment type
                    connect.handleCallResponse(htmlReturn);
                }
            },
            complete: function(response){
                    setTimeout(function(){ connect.checkCallExpert() }, 3000); 
            }
        });
    },
    checkCallUser : function(){
        //run realtime check call
        $.ajax({
            type: 'GET',
            url: '/CallUser/checkCallUser',
            data: {
                'timestamp' : connect.timestamp
            },
            dataType: 'json',
            success: function(data){
                if(data.code == 1){
                    var htmlReturn = '';
                    connect.timestamp = data.timescreate;
                    if(data.typeConfirm == 0){
                        //cancel
                        htmlReturn = '<span>'+data.unameExpertCall+' hiện đang bận. Bạn có muốn đăng ký vào danh sách chờ hay không?</span>';
                        htmlReturn += '<p><input type="button" value="Bỏ qua" class="peding-call-cancel" /><input type="button" value="Ðồng ý" class="pending-call-ok" /></p>';
                        htmlReturn += '<input type="hidden" class="uId" value="'+data.uIdExpert+'"/>';
                        connect.handleResponse(htmlReturn,0);
                    }else{
                        //OK
                        htmlReturn = '<span>'+data.unameExpertCall+' yêu cầu bạn thanh toán. Bạn có đồng ý không?</span>';
                        if(data.paidByHour == 1 && data.turnBasedPayment == 1){
                            htmlReturn += '<p><input type="radio" class="typePayment" name="typePayment" value="1" /><span>Thanh toán theo giờ</span></p>';
                            htmlReturn += '<p><input type="radio" class="typePayment" name="typePayment" value="2" /><span>Thanh toán theo lượt</span></p>';
                        }else if(data.paidByHour == 1){
                            htmlReturn += '<p><input type="hidden" class="typePayment" name="typePayment" value="1" /><span>Thanh toán theo giờ</span></p>';
                        }else if(data.turnBasedPayment == 1){
                            htmlReturn += '<p><input type="hidden" class="typePayment" name="typePayment" value="2" /><span>Thanh toán theo lượt</span></p>';
                        }else{
                            htmlReturn += '<p><input type="hidden" class="typePayment" name="typePayment" value="3" /><span>Tư vấn miễn phí</span></p>';
                        }
                        htmlReturn += '<p></p>';
                        htmlReturn += '<p><input type="button" value="Bỏ qua" class="cofirm-money-cancel" /><input type="button" value="Ðồng ý" class="cofirm-money-ok" /></p>';
                        htmlReturn += '<input type="hidden" class="uId" value="'+data.uIdExpert+'"/>';
                        connect.handleResponse(htmlReturn,0);
                    }
                }else if(data.code == 2){
                    var htmlReturn = '<span>Tư vấn đã kết thúc!</span>';
                    connect.handleResponse(htmlReturn,2);
                }
            },
            complete: function(response){
                setTimeout(function(){ connect.checkCallUser() }, 3000); 
            }
        });
    },
    confirmCall: function(type){
        //type = 0 : Cancel, type = 1: OK
        $.ajax({
            type: 'GET',
            url: '/CallUser/confirmCall',
            data: {
                'uId'  : connect.uId,
                'type' : type
            },
            dataType: 'json',
            success: function(data){
                connect.hiddenResponse();
            }
        });
    },
    backCall : function(){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/backCall',
            data: {
                'uEx'  : connect.uExId
            },
            dataType: 'json',
            success: function(data){
            }
        });
    },
    getPayment : function(typePayment){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/Payment',
            data: {
                'uEx'        : connect.uExId,
                'typePayment': typePayment
            },
            dataType: 'json',
            success: function(data){
                if(data.code == 1){ // tra ve tong time co the nghe tu van de delay
                    connect.pedingCall(connect.uExId,data.uidcall,data.total_time,data.transaction_id,typePayment,data.skype);
                }else{
                    var htmlRespon = '<span>'+data.msg+'</span>';
                    connect.handleResponse(htmlRespon,1);
                }
            }
        });
    },
    closeCall: function(userId,uidCall,type,transactionId,typePayment){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/Endcall',
            data: {
                'uid'       	 : userId,
                'uidCall'   	 : uidCall,
                'typeClose'		 : type,
                'typePayment'    : typePayment,
                'transactionId'	 :transactionId
            },
            dataType: 'json',
            success: function(data){
            }
        });
        connect.hiddenCallResponse();
        return true;
    },
    pedingCall: function(userId,uidCall,timeDelay,transactionId,typePayment,skype){
        connect.sendCallStats(userId,uidCall,transactionId,typePayment);
        var htmlReturn = '<span>Hệ thống bắt đầu tính phí!</span>';
        htmlReturn += '<p><input type="button" value="Ngừng" class="close_call" /></p>';
        htmlReturn += '<input type="hidden" class="userId" value="'+userId+'"/>';
        htmlReturn += '<input type="hidden" class="uidCall" value="'+uidCall+'"/>';//user thuc hien call
        htmlReturn += '<input type="hidden" class="transactionId" value="'+transactionId+'"/>';//transaction id payment
        htmlReturn += '<input type="hidden" class="typePayment" value="'+typePayment+'"/>';//payment type
        connect.handleCallResponse(htmlReturn);
        setTimeout(function(){window.location.href = 'skype:'+skype+'?call';},1000);
        if(timeDelay != 0 && typePayment != 3){
            var timeDelayRunStop =  timeDelay*1000;
            setTimeout(function(){ 
                connect.closeCall(userId,uidCall,1,transactionId,typePayment)
            }, timeDelayRunStop);
        }
    },
    sendCallStats : function(userId,uidCall,transactionId,typePayment){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/sendCallStats',
            data: {
                'uid'       	 : userId,
                'uidCall'   	 : uidCall,
                'transactionId'	 :transactionId,
                'typePayment'    : typePayment,
            },
            dataType: 'json',
            success: function(data){
            }
        });
    },
    handleResponse: function(response,type){
        if(type == 2){
            $('div.call-center div.content').html('');
            $('div.call-center').css('display','none');
        }
        $('div.note-popup').css('display','block');
        $('div.note-popup div.content').html(response);
        if(type == 1 || type == 2){
            setTimeout(function(){ 
                connect.hiddenResponse() 
            }, 1000);
        }
    },
    hiddenResponse: function(){
        $('div.note-popup div.content').html('');
        $('div.note-popup').hide();
    },
    handleCallResponse: function(response){
        $('div.call-center').css('display','block');
        $('div.call-center div.content').html(response);
    },
    hiddenCallResponse: function(){
        $('div.call-center div.content').html('');
        $('div.call-center').hide();
    },
    checkPendingCall : function(){
        $.ajax({
            beforeSend:function(){
            },
            type: 'GET',
            url: '/CallUser/checkPendingCall',
            data: {},
            dataType: 'json',
            success: function(data){
                return data;
            }
        });
    }
}

//vi du call runtime check pending
setTimeout(connect.checkPendingCall(),1000);